$().ready(function () {
    validateRule();
    selectLoad();
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function selectLoad() {
    var html = "";
    $.ajax({
        url: '/delivery/list',
        success: function (data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].deliveryCode + '">' + data[i].deliveryName + '</option>'
            }
            $(".chosen-select").append(html);
            $(".chosen-select").chosen({
                maxHeight: 200
            });
            //点击事件
            $('.chosen-select').on('change', function (e, params) {
                $("#com").val(params.selected)
            });
        }
    });
}

function update() {
    if ($("#com").val() == '') {
        parent.layer.msg("请选择快递公司");
        return false;
    }
    $.ajax({
        cache: true,
        type: "POST",
        url: "/order/update",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            delivery: {
                required: true
            },
            num: {
                required: true
            }
        },
        messages: {
            delivery: {
                required: icon + "请选择快递公司"
            },
            num: {
                required: icon + "请输入快递单号"
            }
        }
    })
}